package neusoft.example.com.yuejian06.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import neusoft.example.com.yuejian06.Bean.User;
import neusoft.example.com.yuejian06.Model.RegisterModel;
import neusoft.example.com.yuejian06.R;
import neusoft.example.com.yuejian06.RetrofitListener;

public class RegisterActivity extends AppCompatActivity {
    private EditText user_name,user_tel,user_password,user_email;
    private Button register_login_button,register_button;
    private RegisterModel model;
    private Intent intent;
    private RetrofitListener<User> listener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initView();

        listener();

       initEvents();
    }

    public void initView(){
        user_name = (EditText) findViewById(R.id.user_name);
        user_tel = (EditText) findViewById(R.id.user_tel);
        user_password = (EditText) findViewById(R.id.user_password);
        user_email = (EditText) findViewById(R.id.user_email);
        register_login_button= (Button) findViewById(R.id.register_login_button);
        register_button = (Button) findViewById(R.id.register_button);
    }
    public void initEvents(){
        register_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!user_tel.getText().toString().isEmpty()) {
                    model = new RegisterModel();
                    model.register(user_name.getText().toString(),user_tel.getText().toString(), user_password.getText().toString(),user_email.getText().toString(),listener);
                }
            }
        });

        register_login_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                intent = new Intent(RegisterActivity.this,LoginActivity .class);
                startActivity(intent);
            }
        });
    }

    public void listener(){
        listener=new RetrofitListener<User>(){
            @Override
            public void onResponse(User l) {

                Toast.makeText(RegisterActivity.this, l.getSuccess().toString(), Toast.LENGTH_SHORT).show();

                if(l.getSuccess().equals("1")){
                    Toast.makeText(RegisterActivity.this, "注册成功，返回登录", Toast.LENGTH_SHORT).show();
                    intent = new Intent(RegisterActivity.this, LoginActivity.class);
                    startActivity(intent);
                    finish();

                }
                else{
                    Toast.makeText(RegisterActivity.this, "电话号码已被注册", Toast.LENGTH_SHORT).show();
                }
            }
            @Override
            public void onFail(String msg) {
                Toast.makeText(RegisterActivity.this, msg, Toast.LENGTH_SHORT).show();
            }
        };
    }
}
